//
//  YBLoginView.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/7.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift

protocol YBLoginViewDelegate: NSObjectProtocol {
    func loginViewDidClick(_ btnType: YBLoginView.BtnType)
    func loginViewInput(_ inputType: YBLoginRegisterInputView.InputType, content: String)
}

class YBLoginView: UIView {
    
    enum BtnType {
        case login
        case register
        case oneClickLogin
    }
    
    weak var delegate: YBLoginViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubviews([titleLabel, phoneInputView, pwdInputView, loginBtn, registerBtn, oneClickLoginBtn, tipsTextView, checkBtn])
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(kNavigationBarHeight+offset15).priority(.high)
            make.height.equalTo(24)
        }
        
        phoneInputView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(27)
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
        }
        
        pwdInputView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(27)
            make.centerX.equalToSuperview()
            make.top.equalTo(phoneInputView.snp.bottom).offset(25)
        }
        
        checkBtn.snp.makeConstraints { make in
            make.size.equalTo(15)
            make.left.equalTo(29)
            make.top.equalTo(pwdInputView.snp.bottom).offset(38)
        }
        
        tipsTextView.snp.makeConstraints { make in
            make.centerY.equalTo(checkBtn)
            make.left.equalTo(checkBtn.snp.right).offset(6)
            make.right.equalTo(-25)
        }
        
        loginBtn.snp.makeConstraints { make in
            make.top.equalTo(checkBtn.snp.bottom).offset(16)
            make.left.right.equalToSuperview().inset(27)
            make.height.equalTo(45)
            make.centerX.equalToSuperview()
        }
        
        registerBtn.snp.makeConstraints { make in
            make.top.equalTo(loginBtn.snp.bottom).offset(12)
            make.right.equalTo(loginBtn)
        }
        
        oneClickLoginBtn.snp.makeConstraints { make in
            make.left.equalTo(loginBtn)
            make.centerY.equalTo(registerBtn)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var titleLabel = self.bk_addLabel(text: "登录", font: .systemFont(ofSize: 20, weight: .bold), bgColor: .clear, textColor: .lightBlackDarkWhite, align: .center)
    
    private lazy var phoneInputView: YBLoginRegisterInputView = {
        let view = YBLoginRegisterInputView(inputType: .phone)
        #if DEBUG
        view.text = "18912341234"
        #endif
        view.onTextField = { [unowned self] text in
            self.delegate?.loginViewInput(.phone, content: text)
        }
        return view
    }()
    
    private lazy var pwdInputView: YBLoginRegisterInputView = {
        let view = YBLoginRegisterInputView(inputType: .pwd)
        #if DEBUG
        view.text = "123456Aa"
        #endif
        view.onTextField = { [unowned self] text in
            self.delegate?.loginViewInput(.pwd, content: text)
        }
        return view
    }()
    
    private lazy var loginBtn: UIButton = {
        let btn = self.bk_addButton(type: .custom, title: "登录", font: .systemFont(ofSize: 18), bgColor: ColorDef.dark27, titleColor: .white, radius: 8)
        btn.bk_addShadowStyleWith(cornerRadius: 8, corners: .allCorners)
        btn.bk_addTarget { [unowned self] sender in
            self.delegate?.loginViewDidClick(.login)
        }
        return btn
    }()
    
    private lazy var oneClickLoginBtn: UIButton = {
        let btn = self.bk_addButton(title: "手机号一键登录", font: .systemFont(ofSize: 13), bgColor: .clear, titleColor: ColorDef.gray153)
        btn.bk_setEnlargeEdge(10)
        btn.bk_addTarget { [unowned self] sender in
            self.delegate?.loginViewDidClick(.oneClickLogin)
        }
        return btn
    }()
    
    private lazy var registerBtn: UIButton = {
        let btn = self.bk_addButton(title: "去注册", font: .systemFont(ofSize: 13), bgColor: .clear, titleColor: ColorDef.gray153)
        btn.bk_setEnlargeEdge(10)
        btn.bk_addTarget { [unowned self] sender in
            self.delegate?.loginViewDidClick(.register)
        }
        return btn
    }()
    
    lazy var checkBtn: UIButton = {
        let btn = self.bk_addImgSelectButton(iconNormal: "icon_check_normal", iconSelect: "icon_check_select")
        btn.bk_setEnlargeEdge(10)
        btn.bk_addTarget { [unowned self] (sender) in
            sender.isSelected = !sender.isSelected
        }
        return btn
    }()
    
    /// 用户协议
    private lazy var tipsTextView: UITextView = {
        let textV = UITextView()
        textV.backgroundColor = .clear
        textV.textAlignment = .left
        textV.isEditable = false
        textV.isScrollEnabled = false
        textV.delegate = self
        textV.linkTextAttributes = [.foregroundColor: UIColor.lightBlack58DarkLight230, .font: UIFont.systemFont(ofSize: 12, weight: .semibold)]
        textV.attributedText = self.reloadTipsTextAttr()
        return textV
    }()
    
}

// MARK: - Private
extension YBLoginView {
    
    private func reloadTipsTextAttr() -> NSMutableAttributedString {
        let str: NSString = "我已阅读并同意《酷卡云服务协议》" as NSString
        let attrText = NSMutableAttributedString(string: str as String)
        let range0 = str.range(of: "我已阅读并同意")
        let range1 = str.range(of: "《酷卡云服务协议》")
        attrText.addAttributes([.foregroundColor: UIColor.lightBlack58DarkLight230, .link: "agreement://", .font: UIFont.systemFont(ofSize: 12, weight: .semibold)], range: range1)
        attrText.addAttributes([.foregroundColor: ColorDef.gray153, .font: UIFont.systemFont(ofSize: 12)], range: range0)
        return attrText
    }
    
}

// MARK: - UITextViewDelegate代理
extension YBLoginView: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        guard let scheme = URL.scheme else { return true }
        if scheme.contains("agreement") {
            let url = "https://superkuka.com/service-agreement-kuka.html"
            let vc = BKWebViewVC(with: url)
            VisibleNaviCtrl?.pushViewController(vc)
            return false
        }
        return true
    }
}
